// MyClass.cs
// 
// Copyright © 2009 FreeZzaph
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace LibFreeZzaph.Goatlist
{
	
	public class GoatlistPlugin : IFreeZzaphContentProviderPlugin
	{
		private static readonly Regex categoryRegex = new Regex(@"\s*(.+?)\s+-\s+<a\s+href=""([^""]+)"">movie\s+galleries</a>[^<]+?<a href=""([^""]+)"">picture\s+galleries</a>\s+-\s+\1", RegexOptions.Compiled | RegexOptions.IgnoreCase);
		
		private IDictionary<string, CategoryGroup> categoryGroups;
		
		public string Name
		{
			get { return "goatlist"; }
		}
		
		public string Title
		{
			get { return "Goat List - Thousands of Free Sex Pics and Movies"; }
		}
		
		public IDictionary<string, CategoryGroup> CategoryGroups
		{
			get
			{
				if (categoryGroups == null)
				{
					categoryGroups = new Dictionary<string, CategoryGroup>();
					
					Uri categoryUrl = new Uri("http://goatlist.com/");
					//Uri categoryUrl = new Uri("file:///tmp/goatlist.html");
					
					string categoryData;
					try
					{
						categoryData = FreeZzaphUtility.FetchUrl(categoryUrl);
					}
					catch (Exception e)
					{
						throw new PluginException("Could not download category page. Goatlist.com may not be accessible, or the plugin may need to be updated to reflect changes on the web site.", e);
					}
					
					IDictionary<string, Category> imageCategories = new Dictionary<string, Category>();
					IDictionary<string, Category> movieCategories = new Dictionary<string, Category>();
					
					Match matcher = categoryRegex.Match(categoryData);
					while (matcher.Success)
					{
						// Extract data from match
						string decodedName = FreeZzaphUtility.DecodeHtmlEntities(matcher.Groups[1].ToString());
						string name = FreeZzaphUtility.Prettify(decodedName);
						string description = decodedName;
						string movieUrl = matcher.Groups[2].ToString();
						string pictureUrl = matcher.Groups[3].ToString();
						
						// Create picture and movie categories
						GoatlistImageCategory gic = new GoatlistImageCategory(name, description, pictureUrl);
						GoatlistMovieCategory gmc = new GoatlistMovieCategory(name, description, movieUrl);
						imageCategories.Add(gic.Name, gic);
						movieCategories.Add(gmc.Name, gmc);
												
						matcher = matcher.NextMatch();
					}
					
					// Create category groups
					CategoryGroup pictures = new CategoryGroup("pictures", "Picture galleries", imageCategories);
					CategoryGroup movies = new CategoryGroup("movies", "Movie galleries", movieCategories);
					
					categoryGroups.Add(pictures.Type, pictures);
					categoryGroups.Add(movies.Type, movies);
				}
				
				return categoryGroups;
			}
		}
		
		public void Initialize(IPluginManager pm)
		{
		}
		
	}
}
